
package game.view.scene {
	import common.debug.Debug;
	
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import game.manager.StageManager;
	import game.model.SceneModel;
	
	
	/**
	 * TileBitmapData
	 *
	 * @author chris
	 *
	 * Apr 1, 2013
	 */
	public class TileBitmapData{
		private static const TILE_WIDTH:int = 300;
		private static const TILE_HEIGHT:int = 300;
		private var _col:int;
		private var _row:int;
		private var _bitmapData:BitmapData;
		private var _graphics:BitmapData;
		private var _url:String;
		private var _loaded:Boolean = false;
		private var _loadThumb:Boolean = false;
		
		private var _loader:Loader
		private var _urlRequest:URLRequest;
		
		public function TileBitmapData(graphics:BitmapData) {
			this._graphics = graphics;
			this._loader = new Loader();
			this._urlRequest = new URLRequest();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
		}
		
		private function onCompleteHandler(evt:Event):void {
			this._bitmapData = _loader.content["bitmapData"];
			draw();
			if(_loadThumb) {
				_loadThumb = false;
				_urlRequest.url = _url;
				_loader.load(_urlRequest);
			}
				
		}
		
		private function onError(evt:Event):void {
			Debug.error(evt.toString()+"图片加载失败");
		}
		
		public function set thumbUrl(tu:String):void {
			Debug.error(tu);
			_loadThumb = true;
//			ImageLoader.instance.loadImg(tu,this,"bitmapData");
			_urlRequest.url = tu;
			_loader.load(_urlRequest);
		}
		
		public function set url(u:String):void {
//			ImageLoader.instance.loadImg(u,this,"bitmapData");
			_url = u;
		}
		
		public function set col(c:int):void {
			this._col = c;
		}
		public function get col():int {
			return this._col;
		}
		public function set row(r:int):void {
			this._row = r;
		}
		public function get row():int {
			return this._row;
		}
		
		private function draw():void {
			var bufferRect:Rectangle = new Rectangle(SceneModel.inst.center.x - (StageManager.inst.screenWith/2 + TILE_WIDTH),SceneModel.inst.center.y - (StageManager.inst.screenHeight/2 + TILE_HEIGHT)
													,StageManager.inst.screenWith + TILE_WIDTH*2,StageManager.inst.screenHeight + TILE_HEIGHT*2);
			
			var itemX:int = _col * TILE_WIDTH;
			var itemY:int = _row * TILE_HEIGHT;
			
			var sourceRect:Rectangle = new Rectangle(Math.max(bufferRect.left,itemX)-itemX,Math.max(bufferRect.top,itemY)-itemY
													,itemX+TILE_WIDTH - Math.max(bufferRect.left,itemX),itemY+TILE_HEIGHT - Math.max(bufferRect.top,itemY));
			var dp:Point = new Point(Math.max(bufferRect.left,itemX) - bufferRect.left,Math.max(bufferRect.top,itemY) - bufferRect.top);
			
			if(_loadThumb) {
				var thumbBitmapData:BitmapData = new BitmapData(this._bitmapData.width*2,this._bitmapData.height*2);
				var matrix:Matrix = new Matrix();
				matrix.scale(2,2);
				thumbBitmapData.draw(this._bitmapData,matrix);
				this._graphics.copyPixels(thumbBitmapData,sourceRect,dp);
			}else {
				this._graphics.copyPixels(this._bitmapData,sourceRect,dp);
			}
			dispose();
		}
		
		public function dispose():void {
			this._bitmapData.dispose();
		}
	}
}